package com.ivi.car.module.timeselection.persenter;

import android.arch.lifecycle.Lifecycle;
import android.text.format.DateFormat;

import com.ivi.car.base.BasePresenter;
import com.ivi.car.module.timeselection.model.ITimeContract;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.wm.machine.baselibrary.utils.LogUtils;
import com.wm.machine.baselibrary.utils.ToastUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2018/3/8.
 */

public class TimePresenter extends BasePresenter<ITimeContract.View>
        implements ITimeContract.Prensenter {

    private Date baseDate;

    public TimePresenter(ITimeContract.View view, LifecycleProvider<Lifecycle.Event> provider) {
        super(view, provider);
    }
    private static String[] WEEK = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    public static final int WEEKDAYS = 7;
    @Override
    public String showWeek() {
        Calendar c = Calendar.getInstance();
        baseDate = c.getTime();
        return dataSplicing(baseDate);
    }

    @Override
    public String dateSwitch(int days) {
        //时间选择。正数代表加一天。负数代表减一天
        Calendar cal = Calendar.getInstance();
        if (days == 1) {
            cal.setTime(baseDate);
            cal.add(Calendar.DATE, days);
            baseDate = cal.getTime();
            LogUtils.i(baseDate.toString());
            return dataSplicing(baseDate);
        } else {
            Date time = cal.getTime();
            String showTime = dataSplicing(baseDate);
            String nowTime  = dataSplicing(time);
            if (showTime.equals(nowTime)) {
                return null;
            } else {
                cal.setTime(baseDate);
                cal.add(Calendar.DATE, days);
                baseDate = cal.getTime();
                LogUtils.i(baseDate.toString());
                return dataSplicing(baseDate);
            }
        }


    }
    /**
     *
     *
     * @param baseDate 需要转换的日期
     */
    public String dataSplicing(Date baseDate) {
        CharSequence charSequence = DateFormat.format("yyyy年MM月dd日", baseDate);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(charSequence);
        stringBuffer.append("(");
        stringBuffer.append(dateToWeek(baseDate));
        stringBuffer.append(")");
        return stringBuffer.toString();
    }

    @Override
    public String dataSplicing(String baseDate) {
        Date date = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
             date = simpleDateFormat.parse(baseDate);
        } catch (ParseException e) {
            LogUtils.e("时间解析错误");
            e.printStackTrace();
        }
        CharSequence charSequence = DateFormat.format("yyyy年MM月dd日", date);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(charSequence);
        stringBuffer.append("(");
        stringBuffer.append(dateToWeek(date));
        stringBuffer.append(")");
        return stringBuffer.toString();
    }

    @Override
    public boolean dateContrast(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        if (year >= calendar.get(Calendar.YEAR)) {
            if (month >= calendar.get(Calendar.MONTH)) {
                if (day >= calendar.get(Calendar.DAY_OF_MONTH)) {
                    return true;
                }
            }
        }
            return false;
    }

    /**
     * 点击事件处理
     *
     * @param date 需要计算的星期的日期
     * @return 返回星期天数
     */
    public String dateToWeek(Date date) {
        //时间装换为星期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayIndex = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayIndex < 1 || dayIndex > WEEKDAYS) {
            return null;
        }
        return  WEEK[dayIndex - 1];
    }
}
